﻿using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewROI;

namespace LeaderCCSLaserUI.Models.VisionModels
{
    public class VM_BlobRectangle : FunctionBase
    {
        public override bool Run(ref HObject ho_Image, string paramJson, ref HTuple hv_Row, ref HTuple hv_Column, ref HTuple hv_Angle, ref HTuple hv_Score, ref HObject maxRegion, ref HObject ho_SelectedRegion, ref HTuple v1, ref HTuple v2)
        {
            try
            {
                bool r1 = true,r2 = true;
                var MBlobRectangleParam = JsonConvert.DeserializeObject<BlobRectangleParam>(paramJson);
                HOperatorSet.VectorAngleToRigid(MBlobRectangleParam.ST_Row, MBlobRectangleParam.ST_Column, 0, hv_Row, hv_Column, 0, out var homMad2D);
                HOperatorSet.AffineTransPoint2d(homMad2D, MBlobRectangleParam.RecRow1, MBlobRectangleParam.RecColumn1, out var recrow1, out var reccolumn1);
                HOperatorSet.AffineTransPoint2d(homMad2D, MBlobRectangleParam.RecRow2, MBlobRectangleParam.RecColumn2, out var recrow2, out var reccolumn2);
                HOperatorSet.GenRectangle1(out var ho_Rectangle, recrow1, reccolumn1, recrow2, reccolumn2);                
                HOperatorSet.Intensity(ho_Rectangle, ho_Image, out var hv_Mean, out var hv_Deviation);
                HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out var ho_ImageReduced);
                HOperatorSet.Threshold(ho_ImageReduced, out var ho_Region, hv_Mean - hv_Deviation, hv_Mean);
                HOperatorSet.ClosingRectangle1(ho_Region, out var ho_RegionClosing, MBlobRectangleParam.ClosingWidth, MBlobRectangleParam.ClosingWidth);

                HOperatorSet.FillUp(ho_RegionClosing, out var ho_RegionFillUp);

                HOperatorSet.Difference(ho_RegionFillUp, ho_RegionClosing, out var ho_RegionDifference);

                if (MBlobRectangleParam.IsCheckNi)
                {
                    HOperatorSet.RegionFeatures(ho_RegionFillUp, "area", out v1);
                    HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegion, "area", "and", MBlobRectangleParam.SelectRecAreaMin, MBlobRectangleParam.SelectRecAreaMax);
                    HOperatorSet.CountObj(ho_SelectedRegion,out var hv_number);
                    if ((int)(new HTuple(hv_number.TupleGreater(0))) != 0)
                    {
                        r1 = true;
                    }
                    else
                    {
                        r1 = false;
                    }
                }
                else
                {
                    r1 = true;
                }
                if (MBlobRectangleParam.IsCheckLasered)
                {
                    HOperatorSet.RegionFeatures(ho_RegionDifference, "area", out v2);
                    HOperatorSet.SelectShape(ho_RegionDifference, out maxRegion, "area", "and", MBlobRectangleParam.SelectLaserPadAreaMin, MBlobRectangleParam.SelectLaserPadAreaMax);
                    HOperatorSet.CountObj(maxRegion, out var hv_number);
                    if ((int)(new HTuple(hv_number.TupleGreater(0))) != 0)
                    {
                        r2 = false;
                    }
                    else
                    {
                        r2 = true;
                    }
                }
                else
                {
                    r2 = true;
                }
  
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionClosing.Dispose();
                ho_RegionFillUp.Dispose();
                ho_RegionDifference.Dispose();

                hv_Mean.Dispose();
                hv_Deviation.Dispose();
                return r1 && r2;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }

        }
    }
}
